﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardInChina.Data.Entity.Entity;

namespace CardInChina.Data.Entity.Interface
{
    public class CardInfoService
    {
        public IEnumerable<CardInfo> GetHotTopCardInfo(int top)
        {
            using (CICEntities db = new CICEntities())
            {
                return db.CardInfoes
                    .Where(p => p.Hot == 1 && p.OnSale == true && p.CardName != null)
                    .OrderByDescending(p => p.Best)
                    .OrderByDescending(p => p.Hot)
                    .Take(top).ToArray();
            }
        }

        public IEnumerable<CardInfo> GetBestTopCardInfo(int top, string cateogryKey)
        {
            using (CICEntities db = new CICEntities())
            {
                CardInfo[] ret = null;
                if (cateogryKey == null)
                {
                    ret = db.CardInfoes
                    .Where(p => p.Best == 1 && p.OnSale && p.CardName != null)
                    .OrderBy(p => p.OrderValue)
                    .Take(top)
                    .ToArray();
                }
                else
                {
                    ret = db.CardInfoes
                    .Where(p => p.Best == 1 && p.OnSale && p.CardName != null && p.OfCardClass.CategoryKey == cateogryKey)
                    .OrderBy(p => p.OrderValue)
                    .Take(top)
                    .ToArray();
                }
                foreach (var r in ret)
                {
                    r.OfCards = r.OfCards.Where(p => p.OnSale == true).OrderBy(p => p.Price).ToArray();
                }
                return ret;
            }
        }

        public IEnumerable<CardInfo> GetSaleHotTopCardInfo(int top)
        {
            using (CICEntities db = new CICEntities())
            {
                return db.CardInfoes
                    .Where(p => p.OnSale && p.IsSoldHotTop == 1)
                    .OrderBy(p => p.CardName)
                    .Take(top)
                    .ToArray();
            }
        }

        public CardInfo GetCardInfoByIDWithCardLoaded(int id)
        {
            using (CICEntities db = new CICEntities())
            {
                var cardinfo = db.CardInfoes.FirstOrDefault(p => p.CardInfoID == id);
                if (cardinfo != null)
                    cardinfo.OfCards = cardinfo.OfCards.Where(p => p.OnSale).ToArray();
                return cardinfo;
            }

        }

        public CardInfo GetCardByKeywordWithCardLoaded(string cardKey)
        {
            using (CICEntities db = new CICEntities())
            {
                var cardinfo = db.CardInfoes.FirstOrDefault(p => p.CardKey == cardKey);
                if (cardinfo.OfCardClass.CategoryKey == null) throw new Exception("card category is null!");
                if (cardinfo != null)
                    cardinfo.OfCards = cardinfo.OfCards.Where(p => p.OnSale == true).ToArray();
                return cardinfo;
            }
        }

        public IEnumerable<Game> GetGamesOfCardInfo(int cardInfoID)
        {
            using (CICEntities db = new CICEntities())
            {
                CardInfo cardinfo = db.CardInfoes.FirstOrDefault(p => p.CardInfoID == cardInfoID);
                if (cardinfo == null) throw new Exception("card is not found!");
                foreach (var cig in cardinfo.OfCardInGames)
                {
                    if (cig.OfGame != null)
                        yield return cig.OfGame;

                }
            }
        }

        public IEnumerable<CardInfo> GetCardInfoByGame(string gameKey)
        {
            using (CICEntities db = new CICEntities())
            {
                Game game = db.Games.FirstOrDefault(p => p.GameKey == gameKey);
                if (game == null) throw new Exception("game is not found!");
                foreach (var cig in game.OfCardInGames)
                {
                    if (cig.OfCardInfo != null)
                    {
                        var tmp = cig.OfCardInfo.OfCardClass;
                        if (cig.OfCardInfo.OnSale && cig.IsShow == 1)
                            yield return cig.OfCardInfo;
                    }
                }
            }
        }


        public IEnumerable<CardInfo> GetCardInfoesByKeyword(string Keyword)
        {
            using (CICEntities db = new CICEntities())
            {
                string[] keys = Keyword.Split(' ');
                foreach (var t in db.CardInfoes.OrderBy(p => p.CardName).ToArray())
                {
                    foreach (var key in keys)
                    {
                        if (t.CardName.Contains(key))
                        {
                            yield return t;
                            break;
                        }
                    }
                }
            }
        }

    }
}
